# 例子 5: 自定义的文件对象
class MyFile:
    def __init__(self, content):
        self.content = content
        self.position = 0

    def read(self, size=-1):
        if size == -1 or self.position + size > len(self.content):
            result = self.content[self.position:]
            self.position = len(self.content)
        else:
            result = self.content[self.position:self.position + size]
            self.position += size
        return result

        # 使用内置的open函数读取文件内容的方式读取自定义的文件对象


with MyFile("Hello, World!") as f:
    print(f.read())  # 输出: Hello, World!

